﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services
Imports System.IO

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class DMSO
    Inherits System.Web.Services.WebService
    '--------------LOGIN: CODE, PASS, MACHINE-------------------: FILTER BY MACHINE ID
    <WebMethod()> _
   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function _Function01_(ByVal Para01 As String, ByVal Para02 As String, ByVal Para03 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = "Select * from _OUSR with(nolock) where UserCode='" + Para01 + "' and Password='" + Para02 + "'"
            Dim ds As New DataSet
            ds = connect.ObjectGetAll_Query_SAP(str)
            If ds.Tables.Count = 0 Then
                Return "User & Password does not match!"
            Else
                If ds.Tables(0).Rows.Count = 0 Then
                    Return "User & Password does not match!"
                Else
                    If ds.Tables(0).Rows(0)("Status") <> "O" Then
                        Return "This user is inactive!"
                    Else
                        Return ""
                        'If ds.Tables(0).Rows(0)("MachineID").ToString <> Para03 Then
                        '    Return "This machine is not registered for this user!"
                        'Else
                        '    Return ""
                        'End If
                    End If
                End If
            End If
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------TODAY VISIT: USER CODE, ROUTE CODE-------------------" FILTER BY DATE
    <WebMethod()> _
    Public Function _Function02_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = ""
            str = String.Format("Select T0.ID,T0.CardCode,T0.CardName, T0.Address,T0.VisitStatus,T1.Balance  from _ORO2 t0 with(nolock) " _
                        + " join OCRD T1 with(nolock) on T0.cardCode=T1.CardCode where VisitStatus='O' and RouteCode='{0}'", Para02)
            Return connect.ObjectGetAll_Query_SAP_JSON(str)
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------START VISIT: USER CODE, ID, LONG, LAT-------------------: DONE
    <WebMethod()> _
    Public Function _Function03_(ByVal Para01 As String, ByVal Para02 As Integer, ByVal Para03 As Long, ByVal Para04 As Long) As String
        Dim connect As New Connection()
        Try
            Dim ds As DataSet
            Dim str As String = String.Format("Update _ORO2 set Longtitude='{0}', Lattitude='{1}', StartTime=Getdate() where ID={2}", Para03, Para04, Para02)
            ds = connect.ObjectGetAll_Query_SAP(str)
            Return ""
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------TODAY ALL VISIT: USER CODE, ROUTE CODE-------------------: FILTER BY DATE
    <WebMethod()> _
    Public Function _Function04_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = "Select ID,CardCode,CardName, Address from _ORO2 with(nolock) where RouteCode='" + Para02 + "' and datediff(dd,VisitDate,Getdate())=0 "
            Return connect.ObjectGetAll_Query_SAP_JSON(str)
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------TODAY DONE VISIT: USER CODE, ROUTE CODE-------------------: FILTER BY DATE
    <WebMethod()> _
    Public Function _Function05_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = "Select ID,CardCode,CardName, Address from _ORO2 with(nolock) where VisitStatus='C' and RouteCode='" + Para02 + "' and datediff(dd,VisitDate,Getdate())=0 "
            Return connect.ObjectGetAll_Query_SAP_JSON(str)
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------DISPLAY KPI LIST: USER CODE, OUTLET CODE-------------------: FILTER BY DATE, ROUTE
    <WebMethod()> _
    Public Function _Function06_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = ""
            str = str + " select T0.KPICode, T0.KPIName, T1.ItemID,T1.ItemDescription, T1.MinValue, T1.MaxValue"
            str = str + " from _ODKP T0 with(nolock) join _ODK1 T1 with(nolock) on T0.KPICode=T1.KPICode "
            'str = str + " where "
            'str = str + " AND GETDATE() between FromDate and ToDate"

            Return connect.ObjectGetAll_Query_SAP_JSON(str)

        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------DISPLAY KPI UPDATE: USER CODE, OUTLET CODE, XML-------------------
    <WebMethod()> _
    Public Function _Function07_(ByVal Para01 As String, ByVal Para02 As String, ByVal Para03 As String) As String
        Dim connect As New Connection()
        Try
            Return ""
        Catch ex As Exception
            Return "Error"
        End Try
    End Function
    '--------------CUSTOMER CARD LIST: USER CODE, CARD CODE-------------------: FILTER BY OUTLET/USER
    <WebMethod()> _
    Public Function _Function08_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = ""
            str = str + " select ItemCode,ItemName from OITM where SellItem='Y'  "
            Return connect.ObjectGetAll_Query_SAP_JSON(str)
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------CUSTOMER CARD UPDATE: USER CODE, CARD CODE, XML-------------------
    <WebMethod()> _
    Public Function _Function09_(ByVal Para01 As String, ByVal Para02 As String, ByVal Para03 As String) As String
        Dim connect As New Connection()
        Try
            'Dim ret As String
            'ret = oXML.CreateMarketingDocument(Para03, "13", "") 'create Invoice
            'If ret = "" Then
            '    Return connect.ReturnMessage(0, "Successful!")
            'Else
            '    Return connect.ReturnMessage("-1", ret)
            'End If
            Return ""
        Catch ex As Exception
            Return "Eror"
        End Try
    End Function
    '--------------VISIT ACK: ID, REMARK-------------------: DONE
    <WebMethod()> _
    Public Function _Function10_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = String.Format("Update _ORO2 set EndTime=Getdate(),VisitStatus='C', Remark='{1}' where ID={0}", Para01, Para02)
            connect.ObjectGetAll_Query_SAP(str)
            Return ""
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------REPORT LIST: USER CODE-------------------: FILTER & SETUP BY USER/MOBILE
    <WebMethod()> _
    Public Function _Function11_(ByVal Para01 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = ""
            str = str + " SELECT T0.ReportName REPORTID,MenuName REPORTNAME FROM _ORPT T0 WITH(NOLOCK)"
            str = str + " LEFT JOIN _ORP1 T1 WITH(NOLOCK) ON T0.ReportName=T1.ReportName WHERE T1.ID IS NULL"

            Return connect.ObjectGetAll_Query_SAP_JSON(str)

        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------REPORT RUN: USERCODE, REPORTID-------------------DONE
    <WebMethod()> _
    Public Function _Function12_(ByVal Para01 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = ""
            str = str + " SELECT querystr FROM _ORPT T0 WITH(NOLOCK) where reportname='" + Para01 + "'  "
            Dim ds As DataSet = connect.ObjectGetAll_Query_SAP(str)
            If ds.Tables.Count > 0 Then
                If ds.Tables(0).Rows.Count > 0 Then
                    Return connect.ObjectGetAll_Query_SAP_JSON(ds.Tables(0).Rows(0)(0).ToString)
                Else
                    Return "NO DATA"
                End If
            Else
                Return "NO DATA"
            End If

        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------GPS MAP: USER CODE, MACHINE ID-------------------: FILTER BY ROUTE, add status
    <WebMethod()> _
    Public Function _Function13_(ByVal Para01 As String, ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = ""
            str = str + " SELECT [Longtitude],[Lattitude] ,[Title], 'O' [Status] FROM [SAP_TDP].[dbo].[_ODVM] "
            Return connect.ObjectGetAll_Query_SAP_JSON(str)
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------UPLOAD PHOTO: BYTE, ID-------------------: 
    <WebMethod()> _
    Public Function _Function14_(ByVal Para01 As Byte(), ByVal Para02 As String) As String
        Dim connect As New Connection()
        Try
            Dim ms As New MemoryStream(Para01)
            Dim FileName As String = CStr(Para02) + Now.Date.ToString("yyyyMMddHHMMss") + ".jpg"
            Dim fs As New FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Foto/") + Para02, FileMode.Create)
            ms.WriteTo(fs)
            ms.Close()
            fs.Close()
            fs.Dispose()
            Return ""
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------ADD NEW OUTLET" OUTLET NAME, ADDRESS, CONTACT, MOBILE, REMARK-------------------
    <WebMethod()> _
    Public Function _Function15_(ByVal Para01 As String, ByVal Para02 As String, ByVal Para03 As String, ByVal Para04 As String, ByVal Para05 As String) As String
        Dim connect As New Connection()
        Try

            Return ""
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
    '--------------CHANGE PWD:USER CODE, OLD PASS, NEW PASS-------------------: DONE
    <WebMethod()> _
    Public Function _Function16_(ByVal Para01 As String, ByVal Para02 As String, ByVal Para03 As String) As String
        Dim connect As New Connection()
        Try
            Dim str As String = "Select * from _OUSR with(nolock) where UserCode='" + Para01 + "' and Password='" + Para02 + "'"
            Dim ds As New DataSet
            ds = connect.ObjectGetAll_Query_SAP(str)
            If ds.Tables.Count = 0 Then
                Return "User & Password does not match!"
            Else
                If ds.Tables(0).Rows.Count = 0 Then
                    Return "User & Password does not match!"
                Else
                    str = String.Format("UPDATE _OUSR SET PASSWORD='{0}' WHERE USERCODE='{1}'", Para01, Para03)
                    connect.ObjectGetAll_Query_SAP(str)
                    Return ""
                End If
            End If
        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
End Class